﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;
using System.Configuration;

namespace SIRC.Framework.Net
{
    /// <summary>
    /// 网络资源下载的业务逻辑，支持http,https,ftp,file前缀
    /// </summary>
    public class WebDownloader
    {
        public byte[] GetResourceByWebClient(string url)
        {
            WebClient wc = new WebClient();
            Uri uri = new Uri(url);
            byte[] date = wc.DownloadData(uri);
            return date;
        }
        /// <summary>
        /// 根据url获得html文本
        /// </summary>
        /// <param name="pageUrl">资源的url地址</param>
        public string GetContentByUrl(string url)
        {
            WebRequest request = WebRequest.Create(url);
            WebResponse response = request.GetResponse();
            Stream resStream = null;
            StreamReader sr = null;
            string sourceHtml = "";
            try
            {
                resStream = response.GetResponseStream();
                sr = new StreamReader(resStream, System.Text.Encoding.Default);
                sourceHtml = sr.ReadToEnd();
                resStream.Close();
                sr.Close();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                resStream.Close();
                sr.Close();
            }
            return sourceHtml;
        }
    }
}
